package com.lagou.client.View;

import com.lagou.client.ClientIntiClose;
import com.lagou.client.ClientScanner;
import com.lagou.model.TestPaper;
import com.lagou.model.TestScore;
import com.lagou.model.User;
import com.lagou.model.Message;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 学员系统
 */
public class ClientUserView {

    private ClientIntiClose cic;

    public ClientUserView(ClientIntiClose cic) {
        this.cic = cic;
    }

    /**
     * 自定义成员方法实现客户端学员系统登录的功能
     */
    public void clientUserLogin() throws IOException, ClassNotFoundException {
        System.out.println("请输入学员的学号信息：");
        String studentId = ClientScanner.getSc().next();
        System.out.println("请输入学员的密码信息:");
        String password = ClientScanner.getSc().next();
        Message message = new Message("userCheck", new User(studentId, password));
        //2.将UserMessage类型的对象通过对象输出流发送给服务器
        cic.getOos().writeObject(message);
        System.out.println("客户端发送学员信息成功！");
        //3.接收服务器的处理结果并给出提示
        message = (Message) cic.getOis().readObject();
        if ("success".equals(message.getType())) {
            System.out.println("登陆成功，欢迎使用！");
            studentSystem(studentId);
        } else {
            System.out.println("用户名或密码错误！");
        }
    }


    /**
     * 学员系统
     */
    public void studentSystem(String studentId) throws IOException, ClassNotFoundException {
        while (true) {
            System.out.println(" \n\n\t\t   在线考试系统");
            System.out.println("------------------------------------");
            System.out.println(" \n\t\t   学员系统");
            System.out.println("   [1] 用户模块");
            System.out.println("   [2] 考试模块");
            System.out.println("   [0] 退出，返回上一页");
            System.out.println("------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getSc().nextInt();
            switch (choose) {
                case 1:
                    userModule(studentId);
                    break;
                case 2:
                    ExamModule(studentId);
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

    /**
     * 用户模块
     */
    public void userModule(String studentId) throws IOException, ClassNotFoundException {
        while (true) {
            System.out.println(" \n\n\t\t   在线考试系统");
            System.out.println("------------------------------------");
            System.out.println(" \n\t\t   学员系统");
            System.out.println(" \n\t\t\t\t   用户模块");
            System.out.println("   [1] 修改密码");
            System.out.println("   [0] 退出，返回上一页");
            System.out.println("------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getSc().nextInt();
            switch (choose) {
                case 1:
                    updatePassword(studentId);
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }


    /**
     * 修改密码
     */
    public void updatePassword(String studentId) throws IOException, ClassNotFoundException {
        System.out.println("请输入原密码");
        String password = ClientScanner.getSc().next();
        Message message = new Message("userCheck", new User(studentId, password));
        cic.getOos().writeObject(message);
        System.out.println("客户端发送验证学员密码信息成功！");
        message = (Message) cic.getOis().readObject();
        if ("success".equals(message.getType())) {
            System.out.println("请输入新密码");
            String newPassword = ClientScanner.getSc().next();
            message = new Message("userPasswordUpdate", new User(studentId, new User(studentId, newPassword)));
            cic.getOos().writeObject(message);
            System.out.println("客户端发送修改学员密码信息成功！");
            message = (Message) cic.getOis().readObject();
            if ("success".equals(message.getType())) {
                System.out.println("密码修改成功！！");
            } else {
                System.out.println("修改出错，请重新尝试！！");
            }
        } else {
            System.out.println("原密码错误请重新输入！");
        }
    }

    /**
     * 考试模块
     * @param studentId
     */
    public void ExamModule(String studentId) throws IOException, ClassNotFoundException {
        while (true) {
            System.out.println(" \n\n\t\t   在线考试系统");
            System.out.println("------------------------------------");
            System.out.println(" \n\t\t   学员系统");
            System.out.println(" \n\t\t\t\t   考试模块");
            System.out.println("   [1] 开始考试");
            System.out.println("   [2] 查询成绩");
            System.out.println("   [3] 导出成绩");
            System.out.println("   [0] 退出，返回上一页");
            System.out.println("------------------------------------");
            System.out.println("请选择要进行的业务编号：");
            int choose = ClientScanner.getSc().nextInt();
            switch (choose) {
                case 1:
                    startExam(studentId);
                    break;
                case 2:
                    resultSelect();
                    break;
                case 3:
                    exportResult();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

    /**
     * 开始考试模块
     * @param studentId
     */
    public void startExam(String studentId) throws IOException, ClassNotFoundException {
        System.out.println(" \n\n\t\t   在线考试系统");
        System.out.println("------------------------------------");
        System.out.println(" \n\t\t   考试模块");
        Message message = new Message("selectStudentByStudentId",new User(studentId));
        cic.getOos().writeObject(message);
        message = (Message) cic.getOis().readObject();
        String userName = message.getUser().getUserName();
        System.out.println("欢迎"+userName+"进入考试系统！！");
        System.out.println("准备开始答题：");
        message = new Message("selectAllExamQuestions",new TestPaper());
        cic.getOos().writeObject(message);
        message = (Message) cic.getOis().readObject();
        //系统所有的考题
        List<TestPaper> listTestPaper = message.getListTestPaper();
        //用户所答考题
        List<TestPaper> listTestPaper1 = new ArrayList<>();
        //将系统的考题进行遍历
        for (int i = 0;i<listTestPaper.size();i++){
            TestPaper testPaper = listTestPaper.get(i);
            System.out.println(i+1+"."+testPaper.show());
            System.out.println("请输入正确答案:");
            String correctAnswer = ClientScanner.getSc().next();
            //将用户的答案 和 系统的考题进行拼装成考题对象
            listTestPaper1.add(new TestPaper(testPaper.getTopic(),testPaper.getA(),testPaper.getB(),testPaper.getC(),testPaper.getD(),correctAnswer));
        }
        //将 考试结果信息 进行发送  （用户信息，考题信息）
        message = new Message("testAnswerCheck",new TestScore(new User(studentId,userName,null),listTestPaper1,null));
        cic.getOos().writeObject(message);
        message = (Message) cic.getOis().readObject();
        if ("success".equals(message.getType())) {
            System.out.println("答题完毕，请返回上一页查询成绩！");
        }
    }

    /**
     * 根据学号查询成绩
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public void resultSelect() throws IOException, ClassNotFoundException {
        System.out.println("请输入学号:");
        String studentId = ClientScanner.getSc().next();
        System.out.println("请输入学员的密码信息:");
        String password = ClientScanner.getSc().next();
        Message message = new Message("resultSelect", new TestScore(new User(studentId,null,password),null,null));
        cic.getOos().writeObject(message);
        message = (Message) cic.getOis().readObject();
        if ("success".equals(message.getType())) {
            TestScore testScore = message.getTestScore();
            System.out.println("学号："+testScore.getUser().getStudentId()+"\t\t姓名:"+testScore.getUser().getUserName());
            Map<String, List<Map<Integer, Boolean>>> testScoreList = testScore.getTestScoreList();
            //获取所有的考试时间 map的Key
            for (String key : testScoreList.keySet()){
                System.out.println("考试时间:"+key);
                List<Map<Integer, Boolean>> result = testScoreList.get(key);
                System.out.println("考试结果:");
                for (int i = 0; i < result.size(); i++) {
                    String str =result.get(i).get(i)==true?"正确":"错误";
                    System.out.println((i+1)+"."+str);
                }
            }
        }
    }


    public void exportResult() throws IOException, ClassNotFoundException {
        System.out.println("请输入学号:");
        String studentId = ClientScanner.getSc().next();
        System.out.println("请输入学员的密码信息:");
        String password = ClientScanner.getSc().next();
        Message message = new Message("userCheck", new User(studentId,null,password));
        cic.getOos().writeObject(message);
        message = (Message) cic.getOis().readObject();
        if ("success".equals(message.getType())) {
            System.out.println("请输入要导出的路径:");
            String path = ClientScanner.getSc().next();
            message = new Message("exportResult",path,new User(studentId,null,password));
            cic.getOos().writeObject(message);
            message = (Message) cic.getOis().readObject();
            if ("success".equals(message.getType())) {
                System.out.println("导出成功！");
            }else {
                System.out.println("导出失败！");
            }
        }

    }
}
